package sk.uniba.fmph.asdf.tempt;

import sk.uniba.fmph.asdf.SerializableEntity;
import sk.uniba.fmph.asdf.test.RelativePoint;

/**
 * Trieda specifikuje poziciu a rozmery pola pre vkladanie peciatok.
 *
 * @author Matej
 * @author tim
 * @author Ivor
 * @serial
 */
public class TestFieldDef extends SerializableEntity {

	private static final long serialVersionUID = 1861207261428025349L;

	/**
	 * @serialField
	 */
	private int id;

	/**
	 * @serialField
	 */
	private int parentId;

	/**
	 * hranice pola
	 *
	 * @serialField
	 */
	private RelativePoint topLeft;
	private RelativePoint bottomRight;

	@Override
	public void validateState() {
		validateId(id);
		validatePoint(topLeft);
		validatePoint(bottomRight);
	}

    /**
     * Metoda overi relativny bod.
     * @param relativePoint relativny bod
     */
	private void validatePoint(final RelativePoint relativePoint) {
		// TODO: naozaj to chceme validovat takto? -tim
		if (Math.min(relativePoint.x, relativePoint.y) < 0) {
			throw new IllegalArgumentException(String.valueOf(id));
		}
	}

	/**
	 * @param id
	 *            identifikator
	 * @param topLeft
	 *            globalne suradnice laveho horneho rohu vramci sablony
	 * @param bottomRight
	 *            globalne suradnice praveho dolneho rohu vramci sablony
	 */
	public TestFieldDef(int id, RelativePoint topLeft, RelativePoint bottomRight) {
		this(id, -1, topLeft, bottomRight);
	}

	/**
	 * @param id
	 *            identifikator
	 * @param parentId
	 *            identifikator rodicovskej oblasti v pripade rozsirenia
	 * @param topLeft
	 *            globalne suradnice laveho horneho rohu vramci sablony
	 * @param bottomRight
	 *            globalne suradnice praveho dolneho rohu vramci sablony
	 */
	public TestFieldDef(int id, int parentId, RelativePoint topLeft, RelativePoint bottomRight) {
		this.id = id;
		this.parentId = parentId;
		this.topLeft = topLeft;
		this.bottomRight = bottomRight;
	}

	/**
	 * Cielom metody je zistit, ci konkretny bod patri do pola.
	 *
	 * @param relativePoint
	 *            bod zaujmu
	 * @return true ak sa bod nachadza v poli, inak false
	 */
	public boolean containsPoint(RelativePoint relativePoint) {
		return (relativePoint.x >= topLeft.x) && (relativePoint.x <= bottomRight.x)
				&& (relativePoint.y >= topLeft.y) && (relativePoint.y <= bottomRight.y);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getParentId() {
		return parentId;
	}

	public RelativePoint getTopLeft() {
		return topLeft;
	}

	public RelativePoint getBottomRight() {
		return bottomRight;
	}

	public void setTopLeft(RelativePoint topLeft) {
		this.topLeft = topLeft;
	}

	public void setBottomRight(RelativePoint bottomRight) {
		this.bottomRight = bottomRight;
	}

	@Override
	public boolean equals(Object obj) {
		TestFieldDef other = (TestFieldDef) obj;
		return (topLeft.equals(other.topLeft) && bottomRight
				.equals(other.bottomRight));
	}
}
